<template>
  <Dialog v-model="dialogVisible" title="确认发货" width="400px">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" label-width="100px">
      <el-form-item label="配送方式">
        <el-radio-group v-model="deliveryType" class="delivery-radio-group w-full">
          <el-radio border value="store" class="delivery-radio-item">
            <div class="flex items-center">
              <Icon icon="ep:shop" class="mr-2" />
              门店配送
            </div>
          </el-radio>
          <el-radio border value="run" class="delivery-radio-item">
            <div class="flex items-center">
              <Icon icon="ep:bicycle" class="mr-2" />
              跑腿配送
            </div>
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="订单号">
        <span class="text-gray-600">{{ orderNo }}</span>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import * as TradeOrderApi from '@/api/mall/trade/order'

defineOptions({ name: 'OrderConfirmDeliveryForm' })

const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中
const deliveryType = ref('store') // 配送方式：store-门店配送，run-跑腿配送
const orderNo = ref('') // 订单号
const formData = ref<{
  id: number | undefined
}>({
  id: undefined // 订单编号
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (row: TradeOrderApi.OrderVO) => {
  resetForm()
  // 设置数据
  formData.value.id = row.id || undefined
  orderNo.value = row.no || ''
  dialogVisible.value = true
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    const orderId = formData.value.id
    if (!orderId) {
      message.error('订单编号不能为空')
      return
    }
    
    // 根据配送方式调用不同的接口
    if (deliveryType.value === 'store') {
      await TradeOrderApi.storeDeliveryOrder(orderId)
      message.success('门店配送发货成功')
    } else if (deliveryType.value === 'run') {
      await TradeOrderApi.runDeliveryOrder(orderId)
      message.success('跑腿配送发货成功')
    }
    
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', true)
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  deliveryType.value = 'store'
  orderNo.value = ''
  formData.value = {
    id: undefined
  }
  formLoading.value = false
}
</script>

<style scoped>
.delivery-radio-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.delivery-radio-item {
  width: 100%;
  margin: 0 !important;
}

:deep(.delivery-radio-item.el-radio) {
  width: 100%;
  height: auto;
  padding: 12px 20px;
  margin-right: 0 !important;
  display: flex;
  align-items: center;
}

:deep(.delivery-radio-item .el-radio__label) {
  width: 100%;
  display: flex;
  align-items: center;
  padding-left: 10px;
}
</style>

